package cn.piaoyi.web.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import cn.piaoyi.bean.Admin;
import cn.piaoyi.bean.Category;
import cn.piaoyi.bean.Daily;
import cn.piaoyi.bean.Goods;
import cn.piaoyi.bean.Price;
import cn.piaoyi.comm.Constant;
import cn.piaoyi.comm.Util;
import cn.piaoyi.exception.PiaoyiException;
import cn.piaoyi.service.AdminService;
import cn.piaoyi.service.CategoryService;
import cn.piaoyi.service.DailyService;
import cn.piaoyi.service.GoodsService;
import cn.piaoyi.service.PriceService;
import cn.piaoyi.service.web.PageList;
import cn.piaoyi.service.web.Pages;
import cn.piaoyi.web.servlet.AdminSession;
import cn.piaoyi.web.ui.OptionsInt;
import cn.piaoyi.web.ui.OptionsLong;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.interceptor.SessionAware;

public class AdminPrice extends BaseAction{
	private static final Log logger = LogFactory.getLog(AdminPrice.class);
	
	private GoodsService goodsService;
	private DailyService dailyService;
	private PriceService priceService;

	public GoodsService getGoodsService() {
		return goodsService;
	}

	public void setGoodsService(GoodsService goodsService) {
		this.goodsService = goodsService;
	}
	
	public DailyService getDailyService() {
		return dailyService;
	}

	public void setDailyService(DailyService dailyService) {
		this.dailyService = dailyService;
	}

	public PriceService getPriceService() {
		return priceService;
	}

	public void setPriceService(PriceService priceService) {
		this.priceService = priceService;
	}
	
	


	private long id;			//主键ID
	private long goodsId;		//商品ID
	private long dailyId;		//场次ID
	private String name;		//名称
	private int price;			//价格
	private int goodsNumber;	//商品数量
	
	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public long getGoodsId() {
		return goodsId;
	}

	public void setGoodsId(long goodsId) {
		this.goodsId = goodsId;
	}

	public long getDailyId() {
		return dailyId;
	}

	public void setDailyId(long dailyId) {
		this.dailyId = dailyId;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getPrice() {
		return price;
	}

	public void setPrice(int price) {
		this.price = price;
	}

	public int getGoodsNumber() {
		return goodsNumber;
	}

	public void setGoodsNumber(int goodsNumber) {
		this.goodsNumber = goodsNumber;
	}
	
	
	private List priceList;
	private Goods goods;
	private Daily daily;

	public List getPriceList() {
		return priceList;
	}

	public void setPriceList(List priceList) {
		this.priceList = priceList;
	}

	public Daily getDaily() {
		return daily;
	}

	public void setDaily(Daily daily) {
		this.daily = daily;
	}

	public Goods getGoods() {
		return goods;
	}

	public void setGoods(Goods goods) {
		this.goods = goods;
	}

	public String execute() {
		try {
			return this.executeMethod(this.getAction());
		} catch (Exception e) {
			logger.error(e);
			return INPUT;
		}
	}

	/**
	 * 初始页
	 */
	public String index(){
		this.setGoods(this.getGoodsService().findGoodsById(this.goodsId));
		this.setDaily(this.getDailyService().findDailyById(this.dailyId));
		this.setPriceList(this.getPriceService().findPrices(-1,this.dailyId, "id", 0));
		return SUCCESS;
	}
	
	public String add(){
		this.setAction("doadd");
		return INPUT;
	}
	
	public String doadd(){
		//添加价格
		Price p = new Price();
		p.setGoodsId(this.goodsId);
		p.setDailyId(this.dailyId);
		p.setName(this.name);
		p.setPrice(this.price);
		p.setGoodsNumber(this.goodsNumber);
		try {
			this.getPriceService().createPrice(p);
		} catch (PiaoyiException e) {
			this.addActionError(this.getText("error.createrror"));
			return ERROR;
		}
		return "index";
	}
	
	public String update(){
		this.setAction("doupdate");
		Price p = this.getPriceService().findPriceById(this.id);
		this.setGoodsId(p.getGoodsId());
		this.setDailyId(p.getDailyId());
		this.setName(p.getName());
		this.setPrice(p.getPrice());
		this.setGoodsNumber(p.getGoodsNumber());
		return INPUT;
	}
	
	public String doupdate(){
		//添加价格
		Price p = this.getPriceService().findPriceById(this.id);
		p.setGoodsId(this.goodsId);
		p.setDailyId(this.dailyId);
		p.setName(this.name);
		p.setPrice(this.price);
		p.setGoodsNumber(this.goodsNumber);
		try {
			this.getPriceService().updatePrice(p);
		} catch (PiaoyiException e) {
			this.addActionError(this.getText("error.saveerror"));
			return ERROR;
		}
		return "index";
	}
	
	public String del(){
		Price p = this.getPriceService().findPriceById(this.id);
		this.setGoodsId(p.getGoodsId());
		this.setDailyId(p.getDailyId());
		this.getPriceService().removePriceById(this.id);
		return "index";
	}




}
